package cn.stylefeng.guns.sys.modular.system.service;

import cn.hutool.core.util.IdUtil;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.sys.core.util.UUIDUtils;
import cn.stylefeng.guns.sys.modular.system.entity.*;
import cn.stylefeng.guns.sys.modular.system.mapper.*;
import cn.stylefeng.guns.sys.modular.system.model.params.CwbbParam;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class BtyYhlsService extends ServiceImpl<BtyyCwYhlsMapper, BtyyCwYhls> {

    @Resource
    private BtyyCwYhlsMapper btyyCwYhlsMapper;



    /**
     * 获取员工培训列表
     */
    public Page<Map<String, Object>> list(CwbbParam cwbbParam) {
        Page page = LayuiPageFactory.defaultPage();
        return this.baseMapper.list(page,cwbbParam);
    }

    /**
     * 获取银行流水列表（不分页）
     */
    public List<BtyyCwYhls> listExportYhls(CwbbParam cwbbParam) {

        return this.baseMapper.listExportYhls(cwbbParam);
    }

    /**
     * 导入银行流水excel
     */
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public Boolean batchImport(String fileName, MultipartFile file) throws Exception {
        boolean notNull = false;
        if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {
            throw new Exception("上传文件格式不正确");
        }
        boolean isExcel2003 = true;
        if (fileName.matches("^.+\\.(?i)(xlsx)$")) {
            isExcel2003 = false;
        }
        InputStream is = file.getInputStream();
        Workbook wb = null;
        if (isExcel2003) {
            wb = new HSSFWorkbook(is);
        } else {
            wb = new XSSFWorkbook(is);
        }
        //excel中多个sheet表，获取所有sheet表的数量
        int sheets = wb.getNumberOfSheets();
        for (int i = 0; i < sheets; i++) {
            Sheet sheet = wb.getSheetAt(i);
            if (sheet != null) {
                notNull = true;
            }

            int lastRowNum = sheet.getLastRowNum();
            for (int r = 2; r <= lastRowNum; r++) {
                Row row = sheet.getRow(r);
                if (row == null) {
                    continue;
                }
                //获取单元格
                for(int m=1;m<9;m++){
                    Cell cell = row.getCell(m);
                    if (ToolUtil.isNotEmpty(cell)) {
                        cell.setCellType(CellType.STRING);
                    }
                }

                DataFormatter dataFormatter = new DataFormatter();
                String dm = row.getCell(0) == null ? "" : row.getCell(0).getStringCellValue();
                String date = row.getCell(1) == null ? "" : row.getCell(1).getStringCellValue();
                String zc = row.getCell(2) == null ? "" : row.getCell(2).getStringCellValue();
                String sr = row.getCell(3) == null ? "" : row.getCell(3).getStringCellValue();
                String ye = row.getCell(4) == null ? "" : row.getCell(4).getStringCellValue();
                String zy = row.getCell(5) == null ? "" : row.getCell(5).getStringCellValue();
                String hm = row.getCell(6) == null ? "" : row.getCell(6).getStringCellValue();
                String sj = row.getCell(7) == null ? "" : row.getCell(7).getStringCellValue();
                String code = row.getCell(8) == null ? "" : row.getCell(8).getStringCellValue();

            List<BtyyCwYhls>list=baseMapper.selectList(new QueryWrapper<BtyyCwYhls>().eq("dm",dm)
                    .eq("bh",code));
            if(list.size()>0){
                baseMapper.delete(new QueryWrapper<BtyyCwYhls>().eq("dm",dm)
                        .eq("bh",code));
              }
                BtyyCwYhls btyyCwYhls = new BtyyCwYhls();
                btyyCwYhls.setId(UUIDUtils.getId());
                btyyCwYhls.setDm(dm);
                btyyCwYhls.setRq(date);
                btyyCwYhls.setZc(zc);
                btyyCwYhls.setSr(sr);
                btyyCwYhls.setYe(ye);
                btyyCwYhls.setZy(zy);
                btyyCwYhls.setHm(hm);
                btyyCwYhls.setSj(sj);
                btyyCwYhls.setBh(code);

                btyyCwYhlsMapper.insert(btyyCwYhls);
            }
        }
        return notNull;
    }


}
